# ============================================================================
# CommLib Static Library
# ============================================================================
# This library contains common utilities for database operations and other
# shared functionality across the project.
# ============================================================================

cmake_minimum_required(VERSION 3.14)

project(commlib VERSION 1.0.0 LANGUAGES CXX)

# ============================================================================
# Compiler Settings
# ============================================================================
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# ============================================================================
# Path Configuration
# ============================================================================
# Define base paths
set(PLUGINS_PATH ${CMAKE_CURRENT_SOURCE_DIR}/..)
set(TRPC_ROOT_PATH ${PLUGINS_PATH}/trpc)
set(MONGO_DRIVER_PATH ${PLUGINS_PATH}/mongo-cxx-driver)

# ============================================================================
# Include Directories
# ============================================================================
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${MONGO_DRIVER_PATH}/include
    ${MONGO_DRIVER_PATH}/include/mongocxx/v_noabi
    ${MONGO_DRIVER_PATH}/include/bsoncxx/v_noabi
    ${TRPC_ROOT_PATH}/include
)

# ============================================================================
# Link Directories
# ============================================================================
link_directories(
    ${MONGO_DRIVER_PATH}/lib64
    ${TRPC_ROOT_PATH}/lib
    ${TRPC_ROOT_PATH}/lib64
)

# ============================================================================
# Collect Source Files
# ============================================================================
# Collect all .cc and .cpp files recursively
file(GLOB_RECURSE COMMLIB_SOURCES_ALL
    "${CMAKE_CURRENT_SOURCE_DIR}/*.cc"
    "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
)

# Filter out build directory files
set(COMMLIB_SOURCES "")
foreach(SRC_FILE ${COMMLIB_SOURCES_ALL})
    # Exclude files in build directory
    string(FIND "${SRC_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" BUILD_DIR_FOUND)
    if(BUILD_DIR_FOUND EQUAL -1)
        list(APPEND COMMLIB_SOURCES ${SRC_FILE})
    endif()
endforeach()

# Collect all .h and .hpp files recursively
file(GLOB_RECURSE COMMLIB_HEADERS_ALL
    "${CMAKE_CURRENT_SOURCE_DIR}/*.h"
    "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp"
)

# Filter out build directory files
set(COMMLIB_HEADERS "")
foreach(HDR_FILE ${COMMLIB_HEADERS_ALL})
    string(FIND "${HDR_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" BUILD_DIR_FOUND)
    if(BUILD_DIR_FOUND EQUAL -1)
        list(APPEND COMMLIB_HEADERS ${HDR_FILE})
    endif()
endforeach()

# Display collected files
message(STATUS "CommLib source files:")
foreach(SRC_FILE ${COMMLIB_SOURCES})
    message(STATUS "  - ${SRC_FILE}")
endforeach()

# ============================================================================
# Create Static Library
# ============================================================================
add_library(commlib STATIC 
    ${COMMLIB_SOURCES}
)

# Add include directories for the library
target_include_directories(commlib 
    PUBLIC 
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/dbagent
    PRIVATE
        ${MONGO_DRIVER_PATH}/include
        ${MONGO_DRIVER_PATH}/include/mongocxx/v_noabi
        ${MONGO_DRIVER_PATH}/include/bsoncxx/v_noabi
        ${TRPC_ROOT_PATH}/include
)

# Link dependencies
target_link_libraries(commlib 
    PUBLIC
        mongocxx
        bsoncxx
        protobuf
)

# Set library properties
set_target_properties(commlib PROPERTIES
    POSITION_INDEPENDENT_CODE ON
    ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
    OUTPUT_NAME "commlib"
)

# ============================================================================
# Installation Rules (Optional)
# ============================================================================
# Install library
install(TARGETS commlib
    ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)

# Install headers
# install(FILES ${COMMLIB_HEADERS}
#     DESTINATION ${CMAKE_INSTALL_PREFIX}/include/commlib
# )

# ============================================================================
# Summary
# ============================================================================
message(STATUS "")
message(STATUS "========================================")
message(STATUS "Library: ${PROJECT_NAME} v${PROJECT_VERSION}")
message(STATUS "Source files: ${COMMLIB_SOURCES}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C++ standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "Output: libcommlib.a")
message(STATUS "========================================")
message(STATUS "")
